﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.ApplicationBlocks.Data;
using System.Data;
using System.Data.SqlClient;
using WP09S03_OfficeTracking.DAL;
using WP09S03_OfficeTracking.DAL.DSTransactionTableAdapters;
using WP09S03_OfficeTracking.DAL.DSItemTableAdapters;

namespace WP09S03_OfficeTracking.Controller
{
    class TransactionController : ParentClass.ParentController 
    {

        public TransactionController()
            : base()
    {
    }
        public DSTransaction.vw_IssueTransactionDataTable GetIssueTransaction(string issueTranID, string status, string consumable, System.Nullable<System.DateTime> usedStartTimeFrom, System.Nullable<System.DateTime> usedStartTimeTo, System.Nullable<System.DateTime> usedEndTimeFrom, System.Nullable<System.DateTime> usedEndTimeTo, string employeeID, string employeeName, string itemID, string itemName, string categoryID, string brand, string model)
        {
            try
            {
                vw_IssueTransactionTableAdapter tran = new vw_IssueTransactionTableAdapter();
                return tran.GetDataBy(issueTranID, status, consumable, usedStartTimeFrom, usedStartTimeTo, usedEndTimeFrom, usedEndTimeTo, employeeID, employeeName, itemID, itemName, categoryID, brand, model);
            }
            catch (Exception err)
            {
                string s = err.ToString();
                return null;
            }
        }

        public DSTransaction.vw_ReturnTransactionDataTable GetReturnTransaction(string returnTranID, System.Nullable<System.DateTime> createDateFrom, System.Nullable<System.DateTime> createDateTo, string employeeID, string employeeName, string itemID, string itemName, string categoryID, string brand, string model)
        {
            try
            {
                vw_ReturnTransactionTableAdapter tran = new vw_ReturnTransactionTableAdapter();
                return tran.GetDataBy(returnTranID, createDateFrom, createDateTo, employeeID, employeeName, itemID, itemName, categoryID, brand, model);

            }
            catch (Exception)
            {
                throw;
            }
        }

        public DSTransaction.vw_ReplenishTransactionDataTable GetReplenishTransaction(string returnTranID, System.Nullable<System.DateTime> createDateFrom, System.Nullable<System.DateTime> createDateTo, string itemID, string itemName, string categoryID, string brand, string model)
        {
            try
            {
                vw_ReplenishTransactionTableAdapter tran = new vw_ReplenishTransactionTableAdapter();
                return tran.GetDataBy(returnTranID, createDateFrom, createDateTo, itemID, itemName, categoryID, brand, model);

            }
            catch (Exception)
            {
                throw;
            }
        }

        public bool IssueTransactionBookAndEquipment(string employeeID,DataTable tranData)
        {
            try
            {
                string conStr = base.strConnection;

                SqlConnection cn = new SqlConnection(conStr);
                cn.Open();
                SqlTransaction tran = cn.BeginTransaction();

                foreach (DataRow row in tranData.Rows)
                {
                    IssueTransactionTableAdapter issueTran = new IssueTransactionTableAdapter();
                    issueTran.Connection = cn;
                    issueTran.Transaction = tran;

                    issueTran.InsertBookAndEquipment(row["ItemDetailID"].ToString(), employeeID, Convert.ToInt32(row["Quantity"].ToString()), Convert.ToDateTime(row["UsedEndTime"].ToString()));

                    ItemDetailsTableAdapter itemTran = new ItemDetailsTableAdapter();
                    itemTran.Connection = cn;
                    itemTran.Transaction = tran;
                    itemTran.UpdateStatus(row["ItemDetailID"].ToString(), 2);
                }

                tran.Commit();
                cn.Close();
                return true;

            }
            catch (Exception)
            {
                
                throw;
            }
        }

        public bool ReturnTransactionBookAndEquipment(string issueTranID, string itemDetailID, int deviceID, int deviceStatusID, string remarks)
        {
            try
            {
                string conStr = base.strConnection;

                SqlConnection cn = new SqlConnection(conStr);
                cn.Open();
                SqlTransaction tran = cn.BeginTransaction();

                ReturnTransactionTableAdapter returnTran = new ReturnTransactionTableAdapter();
                returnTran.Connection = cn;
                returnTran.Transaction = tran;
                returnTran.Insert(issueTranID, deviceID, remarks);

                IssueTransactionTableAdapter issueTran = new IssueTransactionTableAdapter();
                issueTran.Connection = cn;
                issueTran.Transaction = tran;
                issueTran.UpdateIsReturn(issueTranID);

                ItemDetailsTableAdapter itemDetailTran = new ItemDetailsTableAdapter();
                itemDetailTran.Connection = cn;
                itemDetailTran.Transaction = tran;
                itemDetailTran.UpdateStatus(itemDetailID, 1);


                itemDetailTran.UpdateDeviceStatus(itemDetailID, deviceStatusID);



                tran.Commit();
                cn.Close();
                return true;

            }
            catch (Exception)
            {
                
                throw;
            }
        }

        public bool IssueTransactionSupply(string employeeID, DataTable tranData)
        {
            try
            {
                string conStr = base.strConnection;

                SqlConnection cn = new SqlConnection(conStr);
                cn.Open();
                SqlTransaction tran = cn.BeginTransaction();

                foreach (DataRow row in tranData.Rows)
                {
                    IssueTransactionTableAdapter issueTran = new IssueTransactionTableAdapter();
                    issueTran.Connection = cn;
                    issueTran.Transaction = tran;

                    issueTran.InsertSupply(row["ItemDetailID"].ToString(), employeeID, Convert.ToInt32(row["Quantity"].ToString()));

                    OfficeItemsTableAdapter itemTran = new OfficeItemsTableAdapter();
                    itemTran.Connection = cn;
                    itemTran.Transaction = tran;
                    itemTran.UpdateQuantityByItemID(row["ItemID"].ToString(), Convert.ToInt32(row["Quantity"].ToString()), 1);

                }

                tran.Commit();
                cn.Close();
                return true;
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        public bool ReplenishTransaction(string itemID, int quantity)
        {
            try
            {
                string conStr = base.strConnection;

                SqlConnection cn = new SqlConnection(conStr);
                cn.Open();
                SqlTransaction tran = cn.BeginTransaction();


                ReplenishTransactionTableAdapter replenishTran = new ReplenishTransactionTableAdapter();
                replenishTran.Connection = cn;
                replenishTran.Transaction = tran;

                replenishTran.Insert(itemID, quantity);

                OfficeItemsTableAdapter itemTran = new OfficeItemsTableAdapter();
                itemTran.Connection = cn;
                itemTran.Transaction = tran;
                itemTran.UpdateQuantityByItemID(itemID, quantity, 0);



                tran.Commit();
                cn.Close();
                return true;

            }
            catch (Exception)
            {
                
                throw;
            }
        }
    }
}
